﻿package com.projects.xenia.documents_activities;


import java.util.ArrayList;

import com.projects.xenia.R;
import com.projects.xenia.documents_classes.Documents_Blank;
import com.projects.xenia.global_modules.MainGlobalModule;
import com.projects.xenia.prompts_activities.Activity_NumericPrompt;


import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class Activity_BlankList extends ListActivity {
	
	
	private Documents_Blank DocumentClass;
	
	private Context Ctx;
	private EfficientAdapter adap;	
	
	private String BlankId;
	private String DocId;	
	
	private final int IntentId_Prompt_Numeric=0;	
	
	private ArrayList <BlankListStructure> BlankListStructureData;	
	
	private int SelectedItemIndex;
	
	private TextView BlankDescName; 
	
	private class BlankListStructure {
		
		public String  id;		
		public int position;
		public String field_descr;
		public int fieldtype;
		public int required;	
		public String blank_value;	
		
	}
	
	private void GetBlankListStructureByDoc () {
	
		 try {
			 
			 Cursor BlankListStructureCursor = MainGlobalModule.DBE.rawQuery("select distinct a.id,a.blank_id,a.position,a.field_descr,a.field_type,a.is_required,b.id,b.hdoc_id,b.blank_id,b.blankstructure_id,b.blank_value from xref_BlanksStructure a left outer join xdoc_blanks_t b on b.hdoc_id = ? and a.id = b.blankstructure_id and a.blank_id = b.blank_id where a.blank_id = ? order by a.position", new String [] {DocId,BlankId});
			 
			 BlankListStructureData = new ArrayList<BlankListStructure>();
			 
			 for (int i = 0; i < BlankListStructureCursor.getCount(); i++) {
				 BlankListStructureCursor.moveToPosition(i);
				 BlankListStructure it = new BlankListStructure ();
				 it.id = BlankListStructureCursor.getString(0);
				 it.position = BlankListStructureCursor.getInt(2);
				 it.field_descr = BlankListStructureCursor.getString(3);
				 it.fieldtype = BlankListStructureCursor.getInt(4);
				 it.required = BlankListStructureCursor.getInt(5);
				 it.blank_value = BlankListStructureCursor.getString(10);
				 BlankListStructureData.add(it);				 
				 
			 }
			 
			 BlankListStructureCursor.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);            
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.blanklist_form);

		DocumentClass = (Documents_Blank)MainGlobalModule.DocumentClass;
		
		Bundle DocExtra = getIntent().getExtras();		
	
		 
		BlankId = DocExtra.getString("BlankId");		
		DocId = DocExtra.getString("DocId");	
		
		BlankDescName = (TextView)findViewById(R.id.BlankDescName);
		BlankDescName.setText(DocExtra.getString("BlankDescr"));
		
		GetBlankListStructureByDoc ();
		adap = new EfficientAdapter(this);
		setListAdapter(adap);
		getListView().setOnItemClickListener(BlankList_OnItemClickListener);
		Ctx = this;	
		
	}	
	
	
	private OnItemClickListener BlankList_OnItemClickListener = new OnItemClickListener() {		
		public void onItemClick(android.widget.AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		
			SelectedItemIndex = arg2;
			BlankListStructure it = adap.getItem(arg2);			
			
		    switch (it.fieldtype) {		    
		    
		    	// numeric
				case 1:				
				Intent Numpad = new Intent ();
				Numpad.setClass(Ctx, Activity_NumericPrompt.class);
				
				double ParseValue;
				
				try {									  
					ParseValue = Double.parseDouble(it.blank_value);						
				} catch (Exception e) {
					ParseValue = 0.0;
				}
				
				Numpad.putExtra("Numeric", ParseValue);
				startActivityForResult(Numpad, IntentId_Prompt_Numeric);
				break;
			}
		    
		}

		};	
	
	
	public class EfficientAdapter extends BaseAdapter  {
		
		private LayoutInflater mInflater;
		public Context context;
		 
		public EfficientAdapter(Context context) {
		// Cache the LayoutInflate to avoid asking for a new one each time.
		mInflater = LayoutInflater.from(context);
		this.context = context;
		}
		 
		public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		
		if (convertView == null) {			
			convertView = mInflater.inflate(R.layout.blanklist_list, null);
		}
		
		holder = new ViewHolder();
		
		holder.BlankListPosition = (TextView) convertView.findViewById(R.id.BlankListPosition);
		holder.BlankListDescr = (TextView) convertView.findViewById(R.id.BlankListDescr);
		holder.BlankListValue = (TextView) convertView.findViewById(R.id.BlankListValue);
		
		BlankListStructure it = getItem(position);	
		
		holder.BlankListPosition.setText(String.valueOf(it.position)); 
		holder.BlankListDescr.setText(it.field_descr); 
		holder.BlankListValue.setText(it.blank_value);
		
		return convertView;
		
		}
		 
		class ViewHolder {
			
			TextView BlankListPosition;
			TextView BlankListDescr;		
			TextView BlankListValue;			
		}
		 
				 
		@Override
		public long getItemId(int position) {
			
			return 0;
			
		}
		 
		@Override
		public int getCount() {
		
			return BlankListStructureData.size();
		
		}
		 
		@Override
		public BlankListStructure getItem(int position) {
						
			return BlankListStructureData.get(position);
		
		}
		
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {	
		 
		 switch(requestCode) { 
		    case IntentId_Prompt_Numeric:
		    	if (resultCode==RESULT_OK) {	    		
		    		double NumericValue = data.getDoubleExtra("NumericValue", 0);		    		
		    		BlankListStructure it = adap.getItem(SelectedItemIndex);
		    		it.blank_value = String.valueOf(NumericValue);
		    		BlankListStructureData.set(SelectedItemIndex, it);
		    		
		    		adap.notifyDataSetChanged();	    		
		    		
		    		DocumentClass.Line.id = DocumentClass.Line.id;
		    		DocumentClass.Line.hdoc_id = DocId;		
		    		DocumentClass.Line.blank_id = BlankId;
		    		DocumentClass.Line.blankstructure_id = it.id;
		    		DocumentClass.Line.blank_value = it.blank_value;
		    		
		    		DocumentClass.UpdateDocLine();
		    		
		    	}
		    	break;
		 }		 
		    	
	};	
	
    public boolean onCreateOptionsMenu (Menu menu){
    	menu.add(Menu.NONE,1,Menu.NONE,"Завершить");
		return true;    	
    }
    
    public boolean onOptionsItemSelected (MenuItem item){
    	
    	switch (item.getItemId()) {
    		case 1:
    		finish ();
    	}
    	
    	return (true);
    	
    }
 
    	
}
